﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestServices.Response.SkyDrive;
using RestServices.Response.Windows;

namespace RestServices.API
{
    public class WindowsID
    {
        public string ClientID { get; set; }
        public string ClientSecret { get; set; }
        public string AuthorizationCode { get; set; }
        public string Scope { get; set; }
        public delegate WindowsLogin ShowFormLoginHandler(string urlLogin);
        public event ShowFormLoginHandler ShowFormLogin;
        public string AccessToken { get; private set; }
        public WindowsID(string clientID, string clientSecret, string scope, string authorizationCode)
        {
            ClientID = clientID;
            ClientSecret = clientSecret;
            AuthorizationCode = authorizationCode;
        }

        public WindowsID(string clientID, string clientSecret, string scope)
        {
            ClientID = clientID;
            ClientSecret = clientSecret;
            Scope = scope;
        }

        public WindowsID(string clientID, string clientSecret)
        {
            ClientID = clientID;
            ClientSecret = clientSecret;
            Scope = "wl.signin%20wl.basic%20wl.calendars_update%20wl.contacts_calendars%20wl.skydrive_update%20wl.offline_access";
        }

        public WindowsLogin Login(bool ShowForm)
        {
            return Login("", ShowForm);
        }

        public WindowsLogin Login(string refreshToken, bool ShowForm)
        {
            LiveLoginService loginService = new LiveLoginService();
            WindowsLogin login = null;
            LiveUser user1 = null;

            if (refreshToken != "")
            {
                string urlSkyCode = loginService.RequestRefresh(ClientID, ClientSecret, refreshToken);
                login = ServiceCall.MakeRequestPost<WindowsLogin>(urlSkyCode);
            }
            if (login != null)
            {
                string url1 = loginService.RequestUser(login.AccessToken);
                user1 = ServiceCall.MakeRequestGet<LiveUser>(url1);
                //Console.WriteLine("Nome:" + user1.Name);
            }

            if (user1==null && login == null)
            {
                string urlLogin = loginService.RequestSignIn(ClientID, Scope); //, "5npt7vybuR-6Du-ADracJrrtCVl43bYg");

                Livelogin form = null;
                string code = "";

                if (ShowForm)
                {
                    form = new Livelogin(urlLogin);
                    form.ShowDialog();

                    if (form.DialogResult == System.Windows.Forms.DialogResult.OK)
                    {
                        code = form.SkyDriveData.Code;
                    }
                }
                else
                {
                    WindowsLogin loginCode = ShowFormLogin(urlLogin);
                    if (loginCode != null && loginCode.Code != "")
                        code = loginCode.Code;
                }

                if (code != "")
                {
                    string urlSkyCode = loginService.RequestToken(ClientID, ClientSecret, form.SkyDriveData.Code);
                    login = ServiceCall.MakeRequestPost<WindowsLogin>(urlSkyCode);
                }
            }

            if (login != null && login.RefreshToken != "")
                AccessToken = login.AccessToken;

            return login;
        }

        public LiveUser GetUserData()
        {
            LiveLoginService loginService = new LiveLoginService();
            string url = loginService.RequestUser(AccessToken);
            LiveUser user = ServiceCall.MakeRequestGet<LiveUser>(url);
            return user;
        }
    }
}
